package com.wl.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.application.common.all.Result;
import com.wl.application.common.all.SysLog;
import com.wl.application.entity.User;
import com.wl.application.entity.vo.AllotVo;
import com.wl.application.services.AllotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author wl
 * @version 1.0
 * @date 2022/6/23
 * @description: 给角色分配权限 或者用户
 */
@RestController
@RequestMapping("/allot")
public class AllotController {
    @Autowired
    private AllotService allotService;


    @GetMapping("/PowerInfo/{rid}")
    public Result PowerInfo(@PathVariable("rid") Integer rid){
        return allotService.PowerInfo(rid);
    }



    @PostMapping("/addPower")
    public Result addPower(@RequestBody AllotVo allotVo){
        return Result.ok("授权成功!",allotService.addPower(allotVo));
    }


    @PostMapping("/roleAndUserPage")
    public Result roleAndUser(@RequestBody AllotVo allotVo){
        //联表查询当前角色下所有的用户信息
        Page<User> userListByRid = allotService.getUserListByRid(allotVo);
        return Result.ok(userListByRid);
    }


    /**
     * 分页查询所有未授权的用户
     */
    @PostMapping("/noAllotPower")
    public Result noAllotPower(@RequestBody AllotVo allotVo){
        Page<User> page = allotService.noAllotPower(allotVo);
        return Result.ok(page);

    }

    @SysLog("授权操作")
    @PostMapping("/addRoleToUser/{id}")
    public Result addRoleToUser(@PathVariable("id") Integer rid,@RequestBody List<Integer> users){
        allotService.addRoleToUser(rid,users);
        return Result.ok("角色授予成功!");
    }

    @DeleteMapping("/revokePower/{uid}/{rid}")
    public Result revokePower(@PathVariable("uid")Integer uid,@PathVariable("rid")Integer rid){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("rid",rid);
        wrapper.eq("uid",uid);
        return Result.ok("撤销成功!",allotService.remove(wrapper));
    }

    /**
     * 验证当前用户是否有该权限
     */
    public boolean checkPower() {
        return  allotService.checkPower();
    }
}
